import { LoggerUtility } from '../../logger/logger.utility';
import { MySQLUtils } from '../../utility/mysql.utility';
import { Utility } from '../../utility/utility';
import { DictionaryItem } from '../entity/dictionaryitem';
import { Supplier } from '../entity/supplier';

// tslint:disable:triple-equals
// tslint:disable:max-line-length
export class SupplierHandler {
    public async insert(body: any): Promise<{ flag: boolean, output: any }> {
        return MySQLUtils.getConnection().then(async (connection) => {
            const paraResult = this.insertParse(body);
            if (paraResult.flag) {
                const result = await MySQLUtils.Save(connection, paraResult.output);
                return { flag: result.flag, output: result.newEntity };
            } else {
                return { flag: paraResult.flag, output: paraResult.output };
            }
        });
    }

    public async update(body: any): Promise<{ flag: boolean, output: any }> {
        return MySQLUtils.getConnection().then(async (connection) => {
            const paraResult = this.updateCheck(body);
            if (paraResult.flag) {
                const curItem = await connection.createQueryBuilder(Supplier, 'supplier')
                    .where('supplier.supplier_id = :supplierId', { supplierId: body.supplierId })
                    .getOne();
                if (curItem != null) {
                    curItem.supplierName = body.supplierName;
                    curItem.location = body.location;
                    const result = await MySQLUtils.Save(connection, curItem);
                    return { flag: result.flag, output: result.newEntity };
                } else {
                    LoggerUtility.error('SupplierHandler update no id:' + body.supplierId, undefined);
                    return {
                        flag: false,
                        output: 'SupplierHandler update no id:' + body.supplierId,
                    };
                }
            } else {
                return { flag: paraResult.flag, output: paraResult.output };
            }
        });
    }

    public async delete(query: any): Promise<{ flag: boolean, output: any }> {
        const paraResult = this.deleteCheck(query);
        if (paraResult.flag) {
            return MySQLUtils.getConnection().then(async (connection) => {
                const result = await MySQLUtils.Delete(connection, Supplier, +query.supplierId);
                return { flag: result.flag, output: result.output };
            });
        } else {
            LoggerUtility.warn(paraResult.output);
            return { flag: false, output: paraResult.output };
        }
    }

    public async query(): Promise<{ flag: boolean, output: any }> {
        return MySQLUtils.getConnection().then(async (connection) => {
            const items = await connection.createQueryBuilder(Supplier, 'supplier').getMany();
            return { flag: true, output: items };
        });
    }

    public insertParse(body: any): { flag: boolean, output: any } {
        if (!Utility.String.isBlank(body.supplierName)) {
            try {
                const supplier = new Supplier();
                supplier.supplierName = body.supplierName;
                supplier.location = body.location;
                return { flag: true, output: supplier };
            } catch (error) {
                LoggerUtility.error('SupplierHandler insertParse - supplierName' + body.supplierName, error);
                return { flag: false, output: 'SupplierHandler insertParse - error:' + error };
            }
        } else {
            LoggerUtility.warn('SupplierHandler insertParse parameter unpass.');
            return {
                flag: false,
                output: 'SupplierHandler insertParse - supplierName cannot be empty',
            };
        }
    }

    public updateCheck(body): { flag: boolean, output: DictionaryItem | string } {
        if (!Utility.Number.isBlank(+body.supplierId)) {
            return { flag: true, output: body };
        } else {
            LoggerUtility.warn('SupplierHandler updateCheck parameter unpass.');
            return {
                flag: false,
                output: 'SupplierHandler updateCheck - id cannot be empty',
            };
        }
    }

    public deleteCheck(query: any): { flag: boolean, output: any } {
        if (!Utility.Number.isBlank(+query.supplierId)) {
            return { flag: true, output: query };
        } else {
            return { flag: false, output: 'SupplierHandler deleteCheck - id cannot be empty' };
        }
    }
}
